package mui

import (
	"os"

	"github.com/andlabs/ui"
	_ "github.com/andlabs/ui/winmanifest"
)

var Mainwin *ui.Window

func MakeExcelPage(handleExcel func(string)) ui.Control {
	hbox := ui.NewHorizontalBox()
	hbox.SetPadded(true)

	vbox := ui.NewVerticalBox()
	vbox.SetPadded(true)
	hbox.Append(vbox, false)

	hbox.Append(ui.NewVerticalSeparator(), false)

	vbox = ui.NewVerticalBox()
	vbox.SetPadded(true)
	hbox.Append(vbox, true)

	group := ui.NewGroup("配置项")
	group.SetMargined(true)
	vbox.Append(group, true)

	group.SetChild(ui.NewNonWrappingMultilineEntry())

	entryForm := ui.NewForm()
	entryForm.SetPadded(true)
	group.SetChild(entryForm)

	texts := ui.NewEntry()
	RowH := ui.NewEntry()
	RowH.SetText("50")
	sheetName := ui.NewEntry()
	texts.SetText("A")
	sheetName.SetText("Sheet1")

	entryForm.Append("文本所在列", texts, false)
	entryForm.Append("sheet名", sheetName, false)
	entryForm.Append("行高", RowH, false)

	grid := ui.NewGrid()
	grid.SetPadded(true)
	vbox.Append(grid, false)

	button := ui.NewButton("Excel文件")
	entry := ui.NewEntry()
	entry.SetReadOnly(true)
	filename := os.Getenv("excel_file")
	entry.SetText(filename)
	button.OnClicked(func(*ui.Button) {
		filename = ui.OpenFile(Mainwin)
		if filename == "" {
			filename = "(cancelled)"
		}
		entry.SetText(filename)
	})
	grid.Append(button,
		0, 0, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)
	grid.Append(entry,
		1, 0, 1, 1,
		true, ui.AlignFill, false, ui.AlignFill)

	okButton := ui.NewButton("清除特殊字符")
	grid.Append(okButton,
		2, 1, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)

	okButton.OnClicked(func(*ui.Button) {
		os.Setenv("subClass", texts.Text())
		os.Setenv("Sheet", sheetName.Text())
		os.Setenv("RowH", RowH.Text())
		handleExcel(filename)
	})

	return hbox
}
